package com.example.simplesvc.concurrent;

public class SynchPrint {


    static  Object L = new Object();
   static int num = 0;
    String msg;
    int maxPrintCount = 10;

    public SynchPrint(String msg) {
        this.msg = msg;
    }
    public void print(int targetNum) {

        for(int i = 0; i < maxPrintCount;) {

            synchronized (L) {
                while (num % 3 != targetNum) {
                    try {
                        L.wait();
                    }catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print(msg);
                num++;
                i++;
                L.notifyAll();
            }

        }

    }

    public static void main(String[] args) {
        new Thread(()-> {
        new SynchPrint("A").print(0);
        }).start();

        new Thread(()-> {
            new SynchPrint("B").print(1);
        }).start();

        new Thread(()-> {
            new SynchPrint("C").print(2);
        }).start();
    }
}
